<template>
  <Card title="查看服务商/承运商">
    <SupplierTabsForm
      v-if="inited"
      ref="supplier-form"
      :list="supplierInfoList"
      :data="supplierData"
      :operation="operation" readonly/>
    <div class="actions">
      <el-button @click="handleBack()">后退</el-button>
    </div>
  </Card>
</template>
<script>
import SupplierTabsForm from '@/components/supplier/SupplierTabsForm';
import {
  querySupplierCarrierById, // 根据供应商id 查询供应商详情
} from '@/api/supplier/management/supplierCarrier';
import {
  queryApprovalSupplierDetail, // 根据审批流请求id, 查询待审批的供应商详情（带新旧对比字段）
} from '@/api/supplier/management/pendingApproval';
import Dict from '@/mixins/supplier/dict';
import { getInitialFieldsValue, compactObj } from '@/pages/supplier/utils'
import {
  PRODUCT_INFORMATION_FIELDS,
  MANAGEMENT_INFORMATION_FIELDS,
  BILLING_INFORMATION_FIELDS,
  BILLING_METHOD_FIELDS,
  FINANCIAL_INFORMATION_FIELDS,
  DELIVERY_INFORMATION_FIELDS,
} from './constants/info'

export default {
  mixins: [Dict],
  components: { SupplierTabsForm },
  data () {
    const { id = 0, processId = 0 } = this.$route.query
    const supplierBill = getInitialFieldsValue([
      PRODUCT_INFORMATION_FIELDS,
      MANAGEMENT_INFORMATION_FIELDS,
    ])
    const supplierContractBill = getInitialFieldsValue([
      BILLING_INFORMATION_FIELDS,
      BILLING_METHOD_FIELDS,
      DELIVERY_INFORMATION_FIELDS,
      FINANCIAL_INFORMATION_FIELDS,
    ])
    supplierBill.mainCategoryId = ''
    supplierBill.managementAreaName = ''
    const supplierPayeeBillList = []
    const supplierPersonBillList = []
    const supplierSettlementBillList = []
    return {
      id,
      processId,
      inited: false,
      loading: false,
      supplierInfoList: [{
        name: '人员信息',
        children: [{
          ref: 'supplier-staff-list',
          type: 'staffList',
          dataSource: ['supplierPersonBillList'],
          rules: [{}]
        }]
      }, {
        name: '商品信息',
        children: [{
          name: '商品信息',
          ref: 'product-information-form',
          formFields: PRODUCT_INFORMATION_FIELDS,
          dataSource: 'supplierBill',
        }],
      }, {
        name: '管理信息',
        children: [{
          name: '管理信息',
          ref: 'management-information-form',
          formFields: MANAGEMENT_INFORMATION_FIELDS,
          dataSource: 'supplierBill',
        }],
      }, {
        name: '合同信息',
        children: [{
          name: '开户行信息',
          ref: 'bank-information-list',
          type: 'bankList',
          dataSource: 'supplierPayeeBillList',
          rules: {}
        }, {
          name: '结算信息',
          ref: 'billing-information-form',
          formFields: BILLING_INFORMATION_FIELDS,
          dataSource: 'supplierContractBill',
        }, {
          name: '结算方式',
          ref: 'settlement-method-list',
          type: 'settlementMethod',
          dataSource: 'supplierSettlementBillList',
        }, {
          name: '',
          ref: 'billing-method-form',
          formFields: BILLING_METHOD_FIELDS,
          dataSource: 'supplierContractBill',
        }, {
          name: '财务信息',
          ref: 'financial-information-form',
          formFields: FINANCIAL_INFORMATION_FIELDS,
          dataSource: 'supplierContractBill',
        }, {
          name: '交付信息',
          ref: 'delivery-information-form',
          formFields: DELIVERY_INFORMATION_FIELDS,
          dataSource: 'supplierContractBill',
        }],
      }],
      supplierData: {
        supplierBill,
        supplierContractBill,
        supplierPayeeBillList,
        supplierPersonBillList,
        supplierSettlementBillList,
      },
      operation: null
    }
  },
  created () {
    const { id, processId, status, type } = this.$route.query
    if (processId) {
      this.diffSupplierData(processId, status, type)
    } else if (id) {
      this.initSupplierData(id)
    } else {
      this.handleBack()
    }
  },
  methods: {
    async initSupplierData (supplierId) {
      const { status, result = [] } = await querySupplierCarrierById({ supplierId })
      if (status === '0' && result) {
        this.setData({
          supplierBill: result.supplier,
          supplierContractBill: result.supplierContract,
          supplierPayeeBillList: result.supplierPayeeList,
          supplierPersonBillList: result.supplierPersonList,
          supplierSettlementBillList: result.supplierSettlementList,
        })
        this.inited = true
      }
    },
    // 获取供应商新旧对比字段
    async diffSupplierData (processId, processDocumentStatus, processDocumentsOperationType) {
      const { status, result } = await queryApprovalSupplierDetail({ processId, processDocumentStatus, processDocumentsOperationType })
      if (status === '0' && result) {
        const { operation = [], supplierBill } = result
        this.operation = operation
        this.setData(supplierBill)
        this.inited = true
      }
    },
    // 初始化表单数据
    setData (data) {
      data = compactObj(data, this.supplierData)
      // 银行数据
      if (data.supplierPayeeBillList && data.supplierPayeeBillList.length) {
        data.supplierPayeeBillList.forEach((item) => {
          item.bank = item.bankName || ''
        })
      }

      for (let [k, v] of Object.entries(data)) {
        if (Array.isArray(v)) {
          if (v.length) {
            this.supplierData[k] = v
          }
        } else if (v) {
          this.supplierData[k] = v
        }
      }
    },
    handleBack () {
      this.$router.go(-1)
    }
  },
}
</script>
